package com.hhw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hhw.entity.User;
import com.hhw.util.DBUtil;





/*分页查询*/
public class PageDao {
		
	public List<User> findByPage(int pageNum, int pageSize){
		try{
			List<User> list=new ArrayList<User>();
			Connection con=DBUtil.getConnection();
			PreparedStatement pstm=con.prepareStatement("select * from user limit ?,?");
			pstm.setInt(1, (pageNum-1)*pageSize);
			pstm.setInt(2, pageSize);
			ResultSet rs=pstm.executeQuery();
			User p=null;
			while(rs.next()){
				p=new User();
				p.setUser_id(rs.getInt(1));
				p.setUser_name(rs.getString(2));
				p.setUser_password(rs.getString(3));
				p.setUser_sex(rs.getString(4));
				p.setUser_phone(rs.getString(5));
				p.setUser_add(rs.getString(6));
				list.add(p);
				System.out.println(p);
			}
			
			return list;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	//统计用户数

	public int findCountByPage(){
		try{
			Connection con=DBUtil.getConnection();
			PreparedStatement pstm=con.prepareStatement("select count(user_id) from user");
			ResultSet rs=pstm.executeQuery();
			int count=0;
			while(rs.next()){
				count=rs.getInt(1);
			}
			return count;
		}catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}
	
	
	
	
	
	
	
	
}
			

